package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");//wht6.cn
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();//连接
        Channel c = con.createChannel();//通信通道
        //在服务器上创建helloworld队列
        c.queueDeclare("helloworld",false,false,false,null);//队列定义

        //创建回调对象
        DeliverCallback deliverCallback = new DeliverCallback(){

            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                      byte[] b=  delivery.getBody();
                      String ss =new String(b);
                System.out.println("收到： "+ss);

            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
        //从helloworld队列接收消息

        //收到的消息会传递给一个回调对象进行处理
        c.basicConsume("helloworld", true,deliverCallback ,cancelCallback);


    }
}
